package com.csdj.t309.paper.controller;

import com.csdj.t309.paper.entity.Tuser;
import com.csdj.t309.paper.service.TuserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
public class TuserController {
    @Resource
    private TuserService tuserService;
    private Logger log = LoggerFactory.getLogger("user");

    @GetMapping("login")
    public String login() {
        return "/login";
    }

   // @ResponseBody
    @PostMapping("login")
    public String login1(@RequestParam String userCode, @RequestParam String userPassword,
                         HttpSession session, Model model, HttpServletResponse response) throws IOException {
        System.out.println(userCode+userPassword+"==================");
        //response.setContentType("text/html;charset=utf-8");
        Tuser tuser=tuserService.login(userCode, userPassword);
        if (tuser != null) {
            session.setAttribute("userCode", tuser.getUserCode());
            model.addAttribute("userCode", (String) session.getAttribute("userCode"));
            log.info("model.userCode=" + model.getAttribute("userCode"));
            return "/index";
        }
        return "tuser";
    }
}
